package cn.itcast.controller;

import cn.itcast.dao.StudentDao;
import cn.itcast.jopo.Classes;
import cn.itcast.jopo.Student;
import cn.itcast.onetomany.OneToManyManager;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {
    @Test
    public void findAll() throws IOException {
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("StudentManger.xml");
        //获取工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取执行sql对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取实现类对象
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        //调用实现类对象的方法接受结果
        List<Student> list = mapper.findAll();
        for (Student student : list) {
            System.out.println(student);
        }
    }
    @Test
    public void addStudent() throws IOException {
        InputStream is = Resources.getResourceAsStream("StudentManger.xml");
        //获取工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取执行sql对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取实现类对象
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student1=new Student();
        student1.setId(9);
        student1.setName("小王");
        student1.setAge(27);
        mapper.addStudent(student1);
        System.out.println(student1.getId());
        sqlSession.close();

    }
    @Test
    public void ss() throws IOException {
        InputStream is = Resources.getResourceAsStream("StudentManger.xml");
        //获取工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取执行sql对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取实现类对象
        OneToManyManager mapper = sqlSession.getMapper(OneToManyManager.class);
        List<Classes> list = mapper.findAll(1);
        for (Classes classes : list) {
            System.out.println(classes);
        }
    }

}
